package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

public class EditPage extends Utils {

    public static String url = "http://127.0.0.1:58080/index.html";

    public EditPage() {
        super(url);
    }

    /*
     * 检查发布帖子成功
     * */
    public void EditSuc() throws InterruptedException {

        /*
         * 将鼠标挪动到帖子内容区域
         * 控制键盘，输入正文内容
         * */

        driver.findElement(By.cssSelector("#bit-forum-content > div.page-header.d-print-none > div > div > div.col-auto.ms-auto.d-print-none > div > a.btn.btn-primary.d-none.d-sm-inline-block.article_post")).click();
        String blogTitle = "自动化测试";
        driver.findElement(By.cssSelector("#article_post_title")).sendKeys(blogTitle);

        //首先先要查找到这个元素
        WebElement ele = driver.findElement((By.cssSelector("#edit-article > div.CodeMirror.cm-s-default.CodeMirror-wrap.CodeMirror-empty > div.CodeMirror-scroll > div.CodeMirror-sizer > div > div > div > div.CodeMirror-code > div > pre")));

        Actions actions = new Actions(driver);

        //perform 的作用，为了在页面看出效果
        Thread.sleep(1000);
        actions.click(ele).perform();
        Thread.sleep(1000);
        actions.moveToElement(ele).sendKeys("键盘鼠标操作输入博客内容").perform();
        Thread.sleep(1000);

        /*
        * 当前页面未加载到最后
        * 导致提交按钮不可见
        * 所以不能通过 selenium 来操作 。
        * 报错是元素未交互
        * 两种方法解决：
        * */
        //方法一：使用js的原生操作来点击
        WebElement element = driver.findElement(By.cssSelector("#article_post_submit"));
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

        //使用页面滚动
//        JavascriptExecutor js = (JavascriptExecutor) driver;
//        js.executeScript("window.scrollTo(0, 0);");
        driver.findElement(By.cssSelector("#article_post_submit")).click();
        Thread.sleep(1000);

        //检查一下是否发布成功
//        String curTitle = driver.findElement(By.cssSelector("#artical-items-body > div:nth-child(1) > div > div.col > div.text-truncate > a > strong")).getText();
//        assert curTitle.equals(blogTitle);

        driver.quit();
    }

}
